<template>
  <ContentWrap>
    <div :class="searchVisible ? 'flex flex-row flex-wrap mb-2' : 'flex flex-row flex-wrap'">
      <el-button v-if="showBack" @click="router.back()">
        <Icon icon="ep:arrow-left" class="mr-5px" />
        返回
      </el-button>
      <el-button plain type="success" @click="searchVisible = !searchVisible">
        <Icon class="mr-5px" icon="ep:operation" />
        {{ searchVisible ? '关闭筛选' : '打开筛选' }}
      </el-button>
      <el-text type="info">
        <span class="ml-5px">{{ searchVisible ? '关闭以下筛选条件' : '点击打开筛选条件' }}</span>
      </el-text>
      <div class="flex-1"></div>
      <el-button @click="resetQuery">
        <Icon class="mr-5px" icon="ep:refresh" />
        刷新列表
      </el-button>
    </div>
    <el-form v-if="searchVisible" ref="searchFormRef" :model="tableData.searchParam" inline>
      <!-- <el-form-item label="关键词" prop="keyword">
          <el-input
            v-model="tableData.searchParam.keyword"
            class="!w-240px"
            placeholder="请输入关键词"
            clearable
            @keyup="handleQuery"
          />
        </el-form-item> -->
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="tableData.searchParam.createTime"
          type="datetimerange"
          value-format="YYYY-MM-DD HH:mm:ss"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon class="mr-5px" icon="ep:search" />
          搜索
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
  <ContentWrap>
    <div class="mt-[5px]">
      <el-table :data="tableData.list" size="large" v-loading="tableData.loading">
        <template #empty>
          <span>{{ !tableData.loading ? '没有相关数据' : '' }}</span>
        </template>
        <el-table-column label="所属用户" min-width="100">
          <template #default="{ row }">
            <div class="flex flex-col items-center justify-center">
              <img
                class="w-[50px] h-[50px] mr-[10px] border border-solid border-gray-50"
                :src="row.user.avatar || '/avatar.png'"
                alt=""
              />
              <span>{{ row.user.nickname || '匿名' }}</span>
              <span>{{ row.user.mobile || '未绑定手机号' }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          v-hasRole="['super_admin', 'platform_admin', 'tenant_admin']"
          label="所属店铺"
          min-width="150"
        >
          <template #default="{ row }">
            <div>{{ row.shop?.name || row.shopId }}</div>
          </template>
        </el-table-column>
        <el-table-column label="红包金额" width="100">
          <template #default="{ row }">
            <div>{{ fenToYuan(row.amount) }}元</div>
          </template>
        </el-table-column>
        <el-table-column label="状态" width="100">
          <template #default="{ row }">
            <div>{{ row.statusName }}</div>
          </template>
        </el-table-column>
        <el-table-column label="发放时间" width="170">
          <template #default="{ row }">
            <div>{{ formatDate(row.grantedTime) }}</div>
          </template>
        </el-table-column>
        <el-table-column label="过期时间" width="170">
          <template #default="{ row }">
            <div>{{ formatDate(row.expiredTime) }}</div>
          </template>
        </el-table-column>
        <el-table-column label="抵扣信息" min-width="200">
          <template #default="{ row }">
            <div v-if="row.usedTradeNo"
              >订单{{ row.usedTradeNo }}于{{ formatDate(row.usedTime) }}抵扣{{
                fenToYuan(row.usedAmount)
              }}元
            </div>
            <div v-else>无</div>
          </template>
        </el-table-column>
        <el-table-column label="备注" min-width="100">
          <template #default="{ row }">
            <div>{{ row.remark || '无' }}</div>
          </template>
        </el-table-column>
        <el-table-column
          label="创建时间"
          align="center"
          prop="createTime"
          :formatter="dateFormatter"
          min-width="170"
        />
      </el-table>
      <Pagination
        v-model:limit="tableData.searchParam.pageSize"
        v-model:page="tableData.searchParam.pageNo"
        :total="tableData.total"
        @pagination="loadTableData"
      />
    </div>
  </ContentWrap>
</template>

<!--suppress HtmlUnknownAttribute -->
<script lang="ts" setup name="RedPacketRecordList">
import * as RedPacketRecordApi from '@/api/promotion/redpacket/RedPacketRecordApi'
import { FormInstance } from 'element-plus'
import { dateFormatter, formatDate } from '@/utils/formatTime'
import { fenToYuan } from '@/utils'

const router = useRouter()

const searchVisible = ref(false)

const tableData = reactive({
  total: 0,
  loading: true,
  list: [],
  searchParam: {
    pageNo: 1,
    pageSize: 10,
    createTime: []
  }
})

const loadTableData = () => {
  tableData.loading = true
  RedPacketRecordApi.getPage({
    ...tableData.searchParam
  })
    .then((res: any) => {
      tableData.loading = false
      tableData.list = res.list
      tableData.total = res.total
    })
    .catch(() => {
      tableData.loading = false
    })
}

const searchFormRef = ref<FormInstance>()

const handleQuery = () => {
  tableData.searchParam.pageNo = 1
  loadTableData()
}

const resetQuery = () => {
  searchFormRef.value?.resetFields()
  handleQuery()
}

const showBack = ref(false)
onMounted(() => {
  handleQuery()
})
</script>
